package Conexao;



import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

import negocio.Requisicao;
import negocio.StatusConexao;




public class ChecagemConexaoRequest implements Runnable {

	private Socket socket;
	private Socket socketPrincipal;
	private StatusConexao statusConexao;
	
	public ChecagemConexaoRequest(Socket socket,Socket socketPrincipal, StatusConexao statusConexao) throws SocketException{

		this.socket.setSoTimeout(600000);
		this.socket=socket;
		this.socketPrincipal=socketPrincipal;
		this.statusConexao=statusConexao;
	}
	
	public ChecagemConexaoRequest(Socket socket){

		this.socket=socket;
	}



	public void run() throws RuntimeException{

		try{
		
			while(!socketPrincipal.isClosed()){
				ObjectOutputStream out= new ObjectOutputStream(socket.getOutputStream()); 
				ObjectInputStream in= new ObjectInputStream(socket.getInputStream());
				
				Requisicao requisicao= new Requisicao(null, null, 1);
				
				out.writeUnshared(requisicao);
				
				in.readUnshared();
			}
			socket.close();
			
		}catch(Exception e){
			this.statusConexao.isConnected=false;
		}
	}
	
	public void checarConexao(){
		try{
			
			while(true){
				
				ObjectOutputStream out= new ObjectOutputStream(socket.getOutputStream()); 
				ObjectInputStream in= new ObjectInputStream(socket.getInputStream());
				
				Requisicao requisicao= new Requisicao(null, null, 1);
				
				out.writeUnshared(requisicao);
				
				socket.setSoTimeout(600000);
				
				in.readUnshared();
			}
			
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
}

